{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DataFrame\n",
    "**series是处理一维数据，dataframe是处理二维数据。**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、 创建DataFrame：\n",
    "**字典转DataFrames**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': [1, 2, 3, 4, 5], 'b': ['L', 'K', 'J', 'M', 'Z'], 'c': array([-1.06904743,  0.82887059,  0.5132024 ,  0.4273755 ,  1.13344737])}\n",
      "            a  b         c\n",
      "2016-01-01  1  L -1.069047\n",
      "2016-01-02  2  K  0.828871\n",
      "2016-01-03  3  J  0.513202\n",
      "2016-01-04  4  M  0.427376\n",
      "2016-01-05  5  Z  1.133447\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "dict_data = {\n",
    "'a' : [1, 2, 3, 4, 5],\n",
    "'b' : ['L', 'K', 'J', 'M', 'Z'],\n",
    "'c' : np.random.randn(5) }\n",
    "print (dict_data)\n",
    "frame_data = pd.DataFrame(dict_data, index=pd.date_range('20160101',periods=5))\n",
    "print (frame_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dic = {}\n",
    "dic['name'] = frame_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#Series组合成DataFrame的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   APPL  GOOG\n",
      "0     2     1\n",
      "1     4     3\n",
      "2     6     5\n",
      "3     8     7\n",
      "4    10     9\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "s_1 = pd.Series([2, 4, 6, 8, 10], name='APPL')\n",
    "s_2 = pd.Series([1, 3, 5, 7, 9], name=\"GOOG\")\n",
    "numbers = pd.concat([s_1, s_2], axis=1)\n",
    "print (numbers)\n",
    "print (type(numbers))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "直接创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randn(5,2),index=range(0,10,2),columns=list('AB'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 2、 修改dataframes的列名、索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['APPL', 'GOOG'], dtype='object')\n",
      "   MSFT  YHOO\n",
      "0     2     1\n",
      "1     4     3\n",
      "2     6     5\n",
      "3     8     7\n",
      "4    10     9\n"
     ]
    }
   ],
   "source": [
    "print (numbers.columns)\n",
    "# 修改列名\n",
    "numbers.columns = ['MSFT', 'YHOO']\n",
    "print (numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RangeIndex(start=0, stop=5, step=1)\n",
      "            MSFT  YHOO\n",
      "2016-01-01     2     1\n",
      "2016-01-02     4     3\n",
      "2016-01-03     6     5\n",
      "2016-01-04     8     7\n",
      "2016-01-05    10     9\n",
      "[[ 2  1]\n",
      " [ 4  3]\n",
      " [ 6  5]\n",
      " [ 8  7]\n",
      " [10  9]]\n"
     ]
    }
   ],
   "source": [
    "print (numbers.index)\n",
    "# 修改索引\n",
    "numbers.index = pd.date_range(\"20160101\",periods=len(numbers))\n",
    "print (numbers)\n",
    "# 读取Dataframe的值\n",
    "print (numbers.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numbers.iloc[2,1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、 访问序列元素\n",
    "**loc works on labels in the index.**\n",
    "\n",
    "**iloc works on the positions in the index (so it only takes integers). **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randn(5,2),index=range(0,10,2),columns=list('AB'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.370168</td>\n",
       "      <td>-1.276846</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.391108</td>\n",
       "      <td>1.909470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.681539</td>\n",
       "      <td>2.207083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.868097</td>\n",
       "      <td>-0.868733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-0.615020</td>\n",
       "      <td>0.623378</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B\n",
       "0  0.370168 -1.276846\n",
       "2 -0.391108  1.909470\n",
       "4 -0.681539  2.207083\n",
       "6  0.868097 -0.868733\n",
       "8 -0.615020  0.623378"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.681539</td>\n",
       "      <td>2.207083</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B\n",
       "4 -0.681539  2.207083"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[[2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.391108</td>\n",
       "      <td>1.90947</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A        B\n",
       "2 -0.391108  1.90947"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[[2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A  B  C\n",
      "a  1  4  7\n",
      "b  2  5  8\n",
      "c  3  6  9\n"
     ]
    }
   ],
   "source": [
    "data = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]},index=[\"a\",\"b\",\"c\"])\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[\"b\",\"B\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[1,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   B  C\n",
       "b  5  8\n",
       "c  6  9"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc['b':'c','B':'C']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   B  C\n",
       "b  5  8\n",
       "c  6  9"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[1:3,1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "print(data.ix[1,1])\n",
    "print(data.ix[\"b\",\"B\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   B  C\n",
      "b  5  8\n",
      "c  6  9\n",
      "   B  C\n",
      "b  5  8\n",
      "c  6  9\n"
     ]
    }
   ],
   "source": [
    "print(data.ix[1:3,1:3])\n",
    "print(data.ix['b':'c','B':'C'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、 布尔索引\n",
    "\n",
    "与Series一样,有时候我们想过滤DataFrame根据一组标准。我们通过索引DataFrame布尔值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     600036.XSHG  600050.XSHG  601318.XSHG\n",
      "datetime                                                  \n",
      "2017-01-04 15:00:00        69.42         8.98        79.87\n",
      "2017-01-05 15:00:00        69.85         9.48        80.02\n",
      "2017-01-06 15:00:00        69.35         9.27        79.38\n",
      "2017-01-10 15:00:00        69.23         9.13        78.90\n",
      "2017-01-11 15:00:00        69.27         8.34        78.90\n"
     ]
    }
   ],
   "source": [
    "print(data.loc[data['600036.XSHG'].pct_change() > data['601318.XSHG'].pct_change()].head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5、 添加、删除列,结合DataFrames /Series\n",
    "\n",
    "当你已经有一个DataFrame的数据,这很好,但同样重要的是能够增加你的数据\n",
    "\n",
    "**添加新数据**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     600036.XSHG  600050.XSHG  601318.XSHG  600519.XSHG\n",
      "datetime                                                               \n",
      "2017-01-03 15:00:00        69.31         8.99        79.89      2078.80\n",
      "2017-01-04 15:00:00        69.42         8.98        79.87      2186.35\n",
      "2017-01-05 15:00:00        69.85         9.48        80.02      2155.78\n",
      "2017-01-06 15:00:00        69.35         9.27        79.38      2180.14\n",
      "2017-01-09 15:00:00        69.23         9.31        79.38      2165.23\n"
     ]
    }
   ],
   "source": [
    "new = pd.read_excel('sz50.xlsx',sheetname='600519.XSHG', index_col='datetime')\n",
    "data['600519.XSHG'] = new.close\n",
    "print(data.head(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**删除某一列**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     600036.XSHG  601318.XSHG  600519.XSHG\n",
      "datetime                                                  \n",
      "2017-01-03 15:00:00        69.31        79.89      2078.80\n",
      "2017-01-04 15:00:00        69.42        79.87      2186.35\n",
      "2017-01-05 15:00:00        69.85        80.02      2155.78\n",
      "2017-01-06 15:00:00        69.35        79.38      2180.14\n",
      "2017-01-09 15:00:00        69.23        79.38      2165.23\n"
     ]
    }
   ],
   "source": [
    "data = data.drop('600050.XSHG', axis=1)\n",
    "print(data.head(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**合并某一列**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     600036.XSHG  601318.XSHG  600519.XSHG   close\n",
      "datetime                                                          \n",
      "2017-01-03 15:00:00        69.31        79.89      2078.80  323.38\n",
      "2017-01-04 15:00:00        69.42        79.87      2186.35  324.61\n",
      "2017-01-05 15:00:00        69.85        80.02      2155.78  330.94\n",
      "2017-01-06 15:00:00        69.35        79.38      2180.14  327.69\n",
      "2017-01-09 15:00:00        69.23        79.38      2165.23  323.46\n"
     ]
    }
   ],
   "source": [
    "gold_stock = pd.read_excel('sz50.xlsx',sheetname='600547.XSHG', index_col='datetime')\n",
    "\n",
    "df=pd.concat([data,gold_stock['close']], axis=1)\n",
    "print(df.head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df.rename(columns={'close':'600547.XSHG'}, inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     600036.XSHG  601318.XSHG  600519.XSHG  600547.XSHG\n",
      "datetime                                                               \n",
      "2017-01-03 15:00:00        69.31        79.89      2078.80       323.38\n",
      "2017-01-04 15:00:00        69.42        79.87      2186.35       324.61\n",
      "2017-01-05 15:00:00        69.85        80.02      2155.78       330.94\n",
      "2017-01-06 15:00:00        69.35        79.38      2180.14       327.69\n",
      "2017-01-09 15:00:00        69.23        79.38      2165.23       323.46\n"
     ]
    }
   ],
   "source": [
    "print(df.head(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 缺失的数据(再一次)\n",
    "\n",
    "把一个真实数据输入DataFrame带给我们与在系列中同样的问题,只是这次更多的维度。我们有和系列相同的方法来访问,如下显示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     600036.XSHG  601318.XSHG  600519.XSHG  600547.XSHG\n",
      "datetime                                                               \n",
      "2017-04-06 15:00:00        73.44        82.76      2435.12          NaN\n",
      "2017-04-07 15:00:00        72.97        81.97      2441.39          NaN\n",
      "2017-04-10 15:00:00        73.01        81.61      2417.22          NaN\n",
      "2017-04-11 15:00:00        73.36        81.03      2394.98          NaN\n",
      "2017-04-12 15:00:00        73.51        80.63      2424.62          NaN\n",
      "2017-04-13 15:00:00        73.01        80.43      2476.43          NaN\n",
      "2017-04-14 15:00:00        72.63        80.38      2456.92          NaN\n",
      "2017-04-17 15:00:00        72.90        80.67      2459.84          NaN\n",
      "2017-04-18 15:00:00        71.78        79.96      2511.41          NaN\n",
      "2017-04-19 15:00:00        71.31        79.51      2521.23          NaN\n",
      "2017-04-20 15:00:00        70.93        80.18      2580.63          NaN\n",
      "2017-04-21 15:00:00        72.51        80.79      2492.65          NaN\n",
      "2017-04-24 15:00:00        73.05        81.01      2478.98          NaN\n",
      "2017-04-25 15:00:00        73.17        81.66      2603.24          NaN\n",
      "2017-04-26 15:00:00        73.71        84.01      2597.03          NaN\n",
      "2017-04-27 15:00:00        73.24        84.71      2621.69          NaN\n",
      "2017-04-28 15:00:00        73.78        85.00      2565.84          NaN\n",
      "2017-05-02 15:00:00        73.36        84.93      2559.81          NaN\n",
      "2017-05-03 15:00:00        73.05        85.11      2594.92          NaN\n",
      "2017-05-04 15:00:00        72.36        84.68      2585.10          NaN\n",
      "2017-05-05 15:00:00        72.51        83.41      2586.47          NaN\n",
      "2017-05-08 15:00:00        72.51        84.59      2516.57          NaN\n",
      "2017-05-09 15:00:00        72.47        85.11      2541.11          NaN\n",
      "2017-05-10 15:00:00        73.40        89.36      2547.88          NaN\n",
      "2017-05-11 15:00:00        74.33        89.07      2560.74          NaN\n",
      "2017-05-12 15:00:00        78.49        91.72      2568.88          NaN\n",
      "2017-05-15 15:00:00        79.80        91.65      2606.84          NaN\n",
      "2017-05-16 15:00:00        79.11        92.05      2673.14          NaN\n",
      "2017-05-17 15:00:00        77.91        90.53      2650.46          NaN\n"
     ]
    }
   ],
   "source": [
    "print(df[df.isnull().values==True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df_na = df.fillna(method='ffill')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     600036.XSHG  601318.XSHG  600519.XSHG  600547.XSHG\n",
      "datetime                                                               \n",
      "2017-04-26 15:00:00        73.71        84.01      2597.03       317.66\n",
      "2017-04-27 15:00:00        73.24        84.71      2621.69       317.66\n",
      "2017-04-28 15:00:00        73.78        85.00      2565.84       317.66\n",
      "2017-05-02 15:00:00        73.36        84.93      2559.81       317.66\n",
      "2017-05-03 15:00:00        73.05        85.11      2594.92       317.66\n",
      "2017-05-04 15:00:00        72.36        84.68      2585.10       317.66\n",
      "2017-05-05 15:00:00        72.51        83.41      2586.47       317.66\n",
      "2017-05-08 15:00:00        72.51        84.59      2516.57       317.66\n",
      "2017-05-09 15:00:00        72.47        85.11      2541.11       317.66\n",
      "2017-05-10 15:00:00        73.40        89.36      2547.88       317.66\n",
      "2017-05-11 15:00:00        74.33        89.07      2560.74       317.66\n",
      "2017-05-12 15:00:00        78.49        91.72      2568.88       317.66\n",
      "2017-05-15 15:00:00        79.80        91.65      2606.84       317.66\n",
      "2017-05-16 15:00:00        79.11        92.05      2673.14       317.66\n",
      "2017-05-17 15:00:00        77.91        90.53      2650.46       317.66\n"
     ]
    }
   ],
   "source": [
    "print(df_na.loc['2017-04-26':'2017-05-17'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6、 dataframe行列计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean: \n",
      " 600036.XSHG      88.889860\n",
      "601318.XSHG     104.962372\n",
      "600519.XSHG    2823.996047\n",
      "dtype: float64\n",
      "std: \n",
      " 600036.XSHG     15.039725\n",
      "601318.XSHG     22.705049\n",
      "600519.XSHG    532.922368\n",
      "dtype: float64\n",
      "       600036.XSHG  601318.XSHG  600519.XSHG\n",
      "count   215.000000   215.000000   215.000000\n",
      "mean     88.889860   104.962372  2823.996047\n",
      "std      15.039725    22.705049   532.922368\n",
      "min      69.230000    78.820000  2078.800000\n",
      "25%      73.380000    81.390000  2397.590000\n",
      "50%      87.370000   107.730000  2834.120000\n",
      "75%     103.450000   120.980000  3053.000000\n",
      "max     121.820000   169.570000  4467.820000\n",
      "                     600036.XSHG  601318.XSHG  600519.XSHG\n",
      "datetime                                                  \n",
      "2017-01-03 15:00:00     1.000000     1.000000     1.000000\n",
      "2017-01-04 15:00:00     1.001587     0.999750     1.051737\n",
      "2017-01-05 15:00:00     1.007791     1.001627     1.037031\n",
      "2017-01-06 15:00:00     1.000577     0.993616     1.048749\n",
      "2017-01-09 15:00:00     0.998846     0.993616     1.041577\n"
     ]
    }
   ],
   "source": [
    "print('mean:','\\n',data.mean(axis=0))\n",
    "print('std:','\\n',data.std(axis=0))\n",
    "print(data.describe())\n",
    "print(onebegin.head(5))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
